//
//  MediaFansViewController.m
//  YongChuanTradingArea
//
//  Created by Vivian on 2017/9/12.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "MediaFansViewController.h"
#import "LBSegmentControl.h"
#import "MediaFansContentCell.h"

static NSString * const MediaFansContentCellID = @"MediaFansContentCell";

@interface MediaFansViewController ()<UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconImgView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *subtitleLabel;
@property (weak, nonatomic) IBOutlet UIButton *promoteBtn;
@property (weak, nonatomic) IBOutlet UIView *controlView;
@property (strong, nonatomic)LBSegmentControl *control;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;

@end

@implementation MediaFansViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"自媒体";
    self.view.backgroundColor = kBgColor;
    [self createUI];
    // Do any additional setup after loading the view from its nib.
}

- (void)createUI{
    self.iconImgView.backgroundColor = kRandomColor;
    self.iconImgView.layer.cornerRadius = 5;
    self.iconImgView.layer.masksToBounds = YES;
    
    self.promoteBtn.layer.cornerRadius = 15;
    self.promoteBtn.layer.masksToBounds = YES;
    self.promoteBtn.layer.borderWidth = 1;
    self.promoteBtn.layer.borderColor = kOrangeColor.CGColor;
    _control = [[LBSegmentControl alloc] initWithFrame:CGRectMake(0, 0, lbScreenWidth, 60)];
    _control.showTrunk = NO;
    _control.showBottomLine = YES;
    [_control addTarget:self action:@selector(segmentAction:)];
    _control.backgroundColor = kWhiteColor;
    _control.selectedColor = kMainColor;
    _control.trunkTintColor = kMainColor;
    _control.showCount = 3;
    _control.lineWidth = AdaptationRatio(25);
    _control.normalFont = 14;
    _control.items = @[@"金粉", @"银粉", @"铜粉"];
    [self.controlView addSubview:_control];
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.layout.itemSize = CGSizeMake(lbScreenWidth, lbScreenHeight - 64 - 122);
    [self.collectionView registerClass:[MediaFansContentCell class] forCellWithReuseIdentifier:MediaFansContentCellID];
}

#pragma mark - btn clicked
- (IBAction)promoteBtnAction:(UIButton *)sender {
}

#pragma mark UICollectionDelegate
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MediaFansContentCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:MediaFansContentCellID forIndexPath:indexPath];
    return cell;
}

- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return AdaptationRatio(0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return AdaptationRatio(0);
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGPoint point = scrollView.contentOffset;
    NSInteger index = point.x / _collectionView.frame.size.width;
    _control.selectedSegmentIndex = index;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    _control.selectedSegmentIndex = indexPath.item;
}

#pragma mark - click action
- (void) segmentAction:(LBSegmentControl *) sender {
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.selectedSegmentIndex inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleDefault;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
